﻿/**
 * 
 * Copyright 2009, 2010 Mike Jamer [email: mikejamer@gmail.com ]
 * 
 * This File is part of Moose IRC
 * 
 * MooseIRC is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * MooseIRC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with MooseIRC.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using System;
using moose.extension;

namespace moose.irc.standard {

    /// <summary>
    /// Raw 001 Response Action,
    /// Important, because it is used to give us our registered nickname,
    /// we also pull the server welcome message from it.  For whatever we might
    /// want to use that for...
    /// </summary>
    [CLSCompliant(true)]
    [Response(
        Name = "moose-rsp-001",
        Description = "Handles IRC Welcome message",
        Author = "Mike MacKenzie-Jamer <email: mikejamer@gmail.com>",
        Version = "0.1")]
    [Trigger(TriggerText = "001")]
    public sealed class raw001Response : IrcAction {

        protected override IrcState valid_states() {
            return IrcState.Connected;
        }

        protected override void action(IrcClient client, IrcMessage message) {
            //Extract the goods from the raw message
            string msg = message.ToString();
            string nick = msg.Substring(msg.Trim().LastIndexOf(' ')).Trim();

            //Assign the server settings, that we will likely need/want later
            client.ServerSettings[IrcServerSettings.SERVER_WELCOME_MESSAGE] = msg;
            client.ServerSettings[IrcServerSettings.NICKANME] = nick;

            //Set IRC Session State to Registered
            IrcState old_state = client.SessionState;
            client.SessionState = IrcState.Registered;

            //client.notify_event(new SessionStateEvent()
            //{
            //    EventType = IrcEvents.server_registered,
            //    Arguments = message.Arguments.ToArray(),
            //    OldState = old_state,
            //    NewState = client.SessionState
            //});

            //Send the original server message, as a status message, so it may be printed in the status window...
            client.echo(msg);

            return;
        }
    }


}
